package com.larry.mr2.reducejoin;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class ReduceJoinMapper extends Mapper<LongWritable, Text,Text,OrderProduct> {

    FileSplit inputSplit = null;

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        inputSplit = (FileSplit) context.getInputSplit();
    }

    private Text k = new Text();
    private OrderProduct v = new OrderProduct();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String line = value.toString();
        String[] datas = line.split("\t");

        String name = inputSplit.getPath().getName();

        if (name.contains("order")){
            k.set(datas[1]);
            v.setOrderId(datas[0]);
            v.setPid(datas[1]);
            v.setAmount(Integer.valueOf(datas[2]));
            v.setPname("");
            v.setFlag("order");
        }else {
            k.set(datas[0]);
            v.setOrderId("");
            v.setPid(datas[0]);
            v.setAmount(0);
            v.setPname(datas[1]);
            v.setFlag("pd");
        }

        context.write(k,v);
    }
}
